(function(Jml, EFactory) {
	"use strict";

	EFactory.extend({
		/** 添加object元素
		 * @param {string} type : jml元素属性对象
		 * @param {object} proto : 模板对象
		 *  @param {string} parent : 视图对象
		 *  @param {object} builder : 模板生成器对象
		 */
		addObject: function(type, proto, parent, builder) {
			this.add(type, proto, parent || 'object');
		},
		/** 添加object元素
		 * @param {string} type : jml元素属性对象
		 * @param {object} proto : 模板对象
		 *  @param {string} parent : 视图对象
		 *  @param {object} builder : 模板生成器对象
		 */
		addControl: function(type, proto, parent, builder) {
			this.add(type, proto, parent || 'control');
		},
		/** 添加ext元素
		 * @param {string} type : jml元素属性对象
		 * @param {object} proto : 模板对象
		 */
		addExt: function(type, proto) {
			this.add(type, proto, 'ext');
		}
	});

	EFactory.add("object", {
		_build: function() {
			var op = this.page.callFn(this.config.option),
				obj;
			if (op.name) {
				obj = this._buildObject(op);
				this.page.setObject(op, obj);
			}
		},
		_buildObject: function(op) {
			var content = this.domTemplate.text(),config;
			if(content)
			{
				config = this.page.buildFn(content, true,true)
			}
			return config;
		}
	})

	//绑定方法

	// function bindEvents(target, events) {
	// 	$.each(events, function(name, event) {
	// 		if (event.target)
	// 			target.on(name, event.target, event.fn);
	// 		else
	// 			target.bind(name, event);
	// 	});
	// }

	EFactory.add("control", {
		_build: function() {
			////debugger;
			var elem = this,
				config = elem.config,
				page = elem.page,
				builder = elem.builder;

			config.buildHtmlAttr();

			var tmpl = elem._buildTemplate();
			if (tmpl == false)
				return;

			if (tmpl) {
				builder.pushHtml(tmpl.formatText(config.htmlAttr));
			} else {
				var tagName = elem.domTemplate[0].tagName;
				builder.pushHtml("<" + tagName + ' ' + config.htmlAttr.prop + ">");

				Jml.compileChildNodes(elem, elem.domTemplate)
				//Ccay.Jml(domTemplate,view,builder);
				builder.pushHtml("</" + tagName + ">");
			}
		},
		_buildTemplate: function() {
			var tmpl, ext = {};
			this.isInitCtr != false && this._attachInit();
			return $.isFunction(this.template) ? this.template() : this.template;
		},
		_attachInit: function() {
			var elem = this;

			elem.page.fnBeforeLoad.add(function() {
				var page = this,
					config = elem.config,
					target = page.findElement(elem);

				config.execute();
				elem._initControl(target, config.option);
				//注册事件
				//config.event && bindEvents(target, config.event);
				//添加扩展小插件编译
				Jml.compileExt(target, config.ext);
				page.fireInitHandler(elem);
			});
		},
		_initControl: function(target, op) {
			if (this.controlName) {
				var ctr = target[this.controlName];
				ctr && ctr.call(target, op);
			}
		}
	})

	//	function bindEvent(elem,behaivors){
	//		var target;
	//		$.each(behaivors,function(targetId,events){
	//			target = elem.find('#' + targetId);
	//			if(target.length){
	//				$.each(events,function(eventName,fn){
	//					target.bind(eventName,fn);
	//				})
	//			}
	//		});
	//	}
	//	EFactory.add("events",{
	//		_build:function(){
	//			var value = this.domTemplate.html(),behaviors;
	//			if(value && (behaviors = this.page.buildFn("return {" + value + "}",true)))
	//			{
	////				if(behaviors =  this.page.callFn(behaviors))
	////				{
	//					this.page.fnBeforeLoad.add(function(){
	//						bindEvent($(this.target),behaviors);
	//					})
	////				}
	//			}
	//		}
	//	});

})(Ccay.Jml, Ccay.Jml.ElementFactory);